//
//  HomeViewController.m
//  1243
//
//  Created by 我的 on 16/3/22.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "HomeViewController.h"
#import "TableViewCellHeader.h"
#import "ShouJiChongZhiViewController.h"
#import "ZhuanZhangViewController.h"
#import "Masonry.h"
#import "AFNetworking.h"
#import "NetWorkTools.h"
#import "UIDevice+DeviceType.h"
#import "SweepViewController.h"
#import "ErWeiMaViewController.h"
#import "JudgeUserLogInTool.h"
#import "RegisterFirstViewController.h"
#import "LoginViewController.h"
#import "ReadLoginData.h"
#import "XRCarouselView.h"
#import "ShoeBagDressViewController.h"
#import "MiaoShaViewController.h"
#import "YiYuanMainViewController.h"
#import "PinTuanViewController.h"
#import "FirstViewTableViewCell.h"
@interface HomeViewController ()<XRCarouselViewDelegate>
{
    NSArray *tableArr;
}
@property (nonatomic,strong) NSArray *imageViewArray;

@property (nonatomic, strong) XRCarouselView *carouselView;
@end

@implementation HomeViewController
- (NSArray *)imageViewArray {
    if (!_imageViewArray) {
        _imageViewArray = @[
                            [UIImage imageNamed:@"bot_1"],
                            [UIImage imageNamed:@"banner_bot1_boom"],
                            [UIImage imageNamed:@"bot_2"],
                            [UIImage imageNamed:@"bot_3"]
                            ];
        //   wantezhubao_TOP
    }
    return _imageViewArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setHeaderView];
    
    [self setNavBarItem];
    [self loadTopImage:nil];
    self.tableView.backgroundColor = BACKGROUND_COLOR;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerClass:[FirstViewTableViewCell class] forCellReuseIdentifier:@"CELL"];
//    NSLog(@"%@",[self userText]);
#warning 接口测试
//    [self loadUserDataTest];
//    [self logIn];
//    [self loadData];
//    NSLog(@"%@",[self getSha1String:@"ADDUSERabc_1qazxsw@"]);
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 180)];
    headerView.backgroundColor = [UIColor whiteColor];
    [self addHeaderView:headerView];
    [self.view addSubview:headerView];
    
}

- (void)loadData {
    
    NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *currentTime = [formatter stringFromDate:[NSDate date]];
    
//    NSLog(@"%@",currentTime);
}

- (void)addHeaderView:(UIView *)headerView {
    
    NSArray *images = @[
                         [UIImage imageNamed:@"wantezhubao_TOP"],
                        [UIImage imageNamed:@"topbanner_1"],
                        [UIImage imageNamed:@"top_2"],
                        [UIImage imageNamed:@"topbanner_3"]
                        ];
    /**
     *  通过代码创建
     */
    self.carouselView = [XRCarouselView carouselViewWithImageArray:images describeArray:nil];
    //设置frame
    self.carouselView.frame = CGRectMake(0, 0, WIDTH, 180);
    //用代理处理图片点击，如果两个都实现，block优先级高于代理
    self.carouselView.delegate = self;
    [self.carouselView setPageColor:[UIColor whiteColor] andCurrentPageColor:[UIColor blueColor]];
    //设置每张图片的停留时间
    _carouselView.time = 2.5;
    [headerView addSubview:_carouselView];
}

#pragma mark XRCarouselViewDelegate
- (void)carouselView:(XRCarouselView *)carouselView didClickImage:(NSInteger)index {
//    NSLog(@"点击了第%ld张图片", index);
    if (index == 1) {
//        [self pushForm:self.navigationController pushToAndHidden:[[ShoeBagDressViewController alloc] init]];
    }
#warning -----------轮播图点击事件
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (NSDictionary *)userText {

    NSString *string = @"{\"Number\":1,\"UserName\":\"13889464583\",\"Password\":\"123456\",\"Buy_Password\":\"123\",\"RealName\":\"test\",\"Email\":\"text@163.com\",\"V_Number\":1,\"V_Name\":\"vname_test\",\"LastLoginTime\":\"2016-04-12T15:47:20.6938427+08:00\",\"PNumber1\":1,\"PName1\":\"test1\",\"PNumber2\":2,\"PName2\":\"test2\",\"PNumber3\":3,\"PName3\":\"test3\",\"UserRole\":\"RoleTest\",\"Money\":0.0,\"fh_Money\":0.0,\"gq_Money\":0.0,\"gwj_Money\":0.0,\"Dl_Money\":0.0,\"Status\":0,\"CreateTime\":\"2016-04-12T15:47:20.6928426+08:00\",\"Remark\":\"测试\"}";
    
    return [NetWorkTools dictionaryWithJsonString:string];
}

- (void)logIn {
    
    /*
        [(null)] - 请求日志:{"MenthodName":"CREATEORDER","MenthodParms":"{\"UserId\":\"1\",\"UserName\":\"18906539950\",\"ProductId\":\"40476\",\"BuyNum\":\"1\",\"ReceiveName\":\"麦西\",\"ReceiveMobile\":\"18906539950\",\"ReceivePhone\":\"\",\"ReveiveProvice\":\"\",\"ReceiveCity\":\"杭州\",\"ReceiveAddress\":\"浙江省杭州市文二路节能公司\"}","Sign":"D979BAD25BE42D2FD4E963402D4177DF9855CDF9"}
     */
    
    NSDictionary *dict = @{
                           @"ToUserNum":@"1002",
                           @"FromUserNum":@"1001",
                           @"TurnMoney":@"10",
                           @"FromPassword":@"222222"
                           };
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"TURNACCOUNT" completeBlock:^(id object) {
//        NSLog(@"%@",object)
        
        NSString *message = [object objectForKey:@"Message"];
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alert show];
        
//        [self cleanTextField];
    }];
    
}




- (void)loadUserDataTest {

}


- (void)setNavBarItem {
    //// 扫一扫
     self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"nav_1"] highImage:[UIImage imageNamed:@"nav_1"] target:self action:@selector(saoYiSaoBtn) imageEdgeInsets:UIEdgeInsetsMake(0, -20, 15, -35) titleEdgeInsets:UIEdgeInsetsMake(20, -50, 0, 0) title:@"扫一扫"];
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_1"] style:UIBarButtonItemStylePlain target:self action:@selector(saoYiSaoBtn)];
    
    
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"nav_3"] highImage:[UIImage imageNamed:@"nav_3"] target:self action:@selector(fuKuanBtn) imageEdgeInsets:UIEdgeInsetsMake(0, 35, 15, -35) titleEdgeInsets:UIEdgeInsetsMake(20, 20, 0, 0) title:@"收款"];
    
//    NSString *strModel = [[UIDevice currentDevice] machine];
    
    CGRect frame;
    if (WIDTH == 320) {
        frame = CGRectMake(-25, 0, 200, 30);
    } else {
        frame = CGRectMake(0, 0, 200, 30);
    }
    UIView *view = [[UIView alloc] initWithFrame:frame];
    UITextField *searchFeild = [[UITextField alloc] initWithFrame:frame];
    searchFeild.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入商家或者商品名称" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    searchFeild.textColor = [UIColor redColor];
    searchFeild.font = [UIFont systemFontOfSize:14];
    searchFeild.layer.cornerRadius = searchFeild.frame.size.height/2;
    searchFeild.layer.masksToBounds = YES;
    searchFeild.enabled = NO;
    searchFeild.layer.borderWidth = 1;
    searchFeild.layer.borderColor = [UIColor colorWithRed:52/255. green:147/255. blue:225/255. alpha:1].CGColor;
    [self setLeftViewWithTextField:searchFeild imageName:@"nav_2"];
    [view addSubview:searchFeild];

    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame = searchFeild.frame;
    [searchBtn addTarget:self action:@selector(searchBut) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:searchBtn];
    
    self.navigationItem.titleView = view;
}

- (void)searchBut {
//    NSLog(@"...");
}

- (void)saoYiSaoBtn {
    if ([JudgeUserLogInTool  JudgeUserLonInStatus]) {
    SweepViewController *a = [[SweepViewController alloc] init];
        [self pushForm:self.navigationController pushToAndHidden:a];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请登录" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        
        [alert show];
        
    }
}

- (void)fuKuanBtn {
    if ([JudgeUserLogInTool  JudgeUserLonInStatus]) {
    ErWeiMaViewController *a = [[ErWeiMaViewController alloc] init];
        [self pushForm:self.navigationController pushToAndHidden:a];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请登录" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        
        [alert show];
        
    }
}

//tabBar 隐藏
- (void)pushForm:(UINavigationController *)pushForm pushToAndHidden:(UIViewController *)pushToVC
{
    [pushToVC setHidesBottomBarWhenPushed:YES];
    [pushForm pushViewController:pushToVC animated:YES];
    
}

/**
 *  给UITextField设置右侧的图片
 *
 *  @param textField UITextField
 *  @param imageName 图片名称
 */

-(void)setLeftViewWithTextField:(UITextField *)textField imageName:(NSString *)imageName{
    
    UIImageView *rightView = [[UIImageView alloc]init];
    
    rightView.image = [UIImage imageNamed:imageName];
    rightView.frame = CGRectMake(0, 0, CGRectGetHeight(textField.frame), CGRectGetHeight(textField.frame));
    rightView.contentMode = UIViewContentModeCenter;
    
    textField.leftView = rightView;
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.backgroundColor = [UIColor blackColor];
    textField.alpha = 0.25;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return tableArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FirstViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, WEITH - 10, 180)];
    imageView.layer.cornerRadius = 10;
    imageView.layer.masksToBounds = YES;
    cell.picImg.image = self.imageViewArray[indexPath.row];
//    cell.contentView.backgroundColor = BACKGROUND_COLOR;
    NSString *URLString = @"http://120.76.162.124:8083";
    NSString *picture = [tableArr[indexPath.row] objectForKey:@"Logo"];
    
    if ([picture isEqual:[NSNull null]]) {
        
    }else{
        NSString *PicString = [URLString stringByAppendingString:picture];
        [cell.picImg sd_setImageWithURL:[NSURL URLWithString:PicString] placeholderImage:[UIImage imageNamed:@""]];
        
        
//        [cell.contentView addSubview:imageView];
    }
    
    cell.jumpMoreBlock = ^void() {
        [self moreAction];
    };
    
    return cell;
}

#pragma mark ---- 更多按钮点击事件
- (void)moreAction {
    
}


#pragma mark - UITableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
     //   [self pushForm:self.navigationController pushToAndHidden:[[ShoeBagDressViewController alloc] init]];
        
        ShoeBagDressViewController * shoes = [[ShoeBagDressViewController alloc] init];
        shoes.arr = [NSString stringWithFormat:@"%@",tableArr[indexPath.row][@"Id"]];
        shoes.payIndex = 0;
        [self pushForm:self.navigationController pushToAndHidden:shoes];
    
}



- (void)setHeaderView {
    //  首页列表点击方法
    TableViewCellHeader *headerView = [TableViewCellHeader loadTableViewCellHeader];
    
   // [headerView.shouJiChongZhiBtn addTarget:self action:@selector(didClickShouJiButton) forControlEvents:UIControlEventTouchUpInside];
    
   // [headerView.zhuanZhangBtn addTarget:self action:@selector(didClickZhuanZhangBtn) forControlEvents:UIControlEventTouchUpInside];
    
 //   [headerView.sheQuFuWuBtn addTarget:self action:@selector(didClickShangChengButtonNotification) forControlEvents:UIControlEventTouchUpInside];
    
    /* 
     @property (weak, nonatomic) IBOutlet UIButton *liCaiBaoBtn;
     @property (weak, nonatomic) IBOutlet UIButton *zhuanZhangBtn;
     @property (weak, nonatomic) IBOutlet UIButton *shouJiChongZhiBtn;
     @property (weak, nonatomic) IBOutlet UIButton *youHuiQuanBtn;
     @property (weak, nonatomic) IBOutlet UIButton *shengHuoJiaoFeiBtn;
     @property (weak, nonatomic) IBOutlet UIButton *sheQuFuWuBtn;

     */
    
    [headerView.youHuiQuanBtn addTarget:self action:@selector(LiCaiBtn) forControlEvents:UIControlEventTouchUpInside];
     [headerView.sheQuFuWuBtn addTarget:self action:@selector(otherAction) forControlEvents:UIControlEventTouchUpInside];
     [headerView.shengHuoJiaoFeiBtn addTarget:self action:@selector(otherAction) forControlEvents:UIControlEventTouchUpInside];
     [headerView.liCaiBaoBtn addTarget:self action:@selector(miaoSha) forControlEvents:UIControlEventTouchUpInside];
     [headerView.shouJiChongZhiBtn addTarget:self action:@selector(PinTuanViewController) forControlEvents:UIControlEventTouchUpInside];
     [headerView.zhuanZhangBtn addTarget:self action:@selector(YiYuanDuoBao) forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView.tableHeaderView = headerView;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.rowHeight = 190;

}


//  [[NSNotificationCenter defaultCenter] postNotificationName:@"DidClickXiaoFeiButtonChangeTabBarControllerSelected" object:nil userInfo:nil];
#pragma mark 点击按钮事件
- (void)miaoSha{
#warning 秒杀
    MiaoShaViewController * shoes = [[MiaoShaViewController alloc] init];
    [self pushForm:self.navigationController pushToAndHidden:shoes];
  
    
}
- (void)YiYuanDuoBao{
#warning 一元夺宝
    YiYuanMainViewController * shoes = [[YiYuanMainViewController alloc] init];
    [self pushForm:self.navigationController pushToAndHidden:shoes];
 
}

- (void)PinTuanViewController{
#warning 拼团
    PinTuanViewController * shoes = [[PinTuanViewController alloc] init];
    [self pushForm:self.navigationController pushToAndHidden:shoes];
    
    }
- (void)otherAction{
    UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"敬请期待" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    
    [alview show];
    
    
}
- (void)LiCaiBtn{
     [[NSNotificationCenter defaultCenter] postNotificationName:@"DidClickXiaoFeiButtonChangeTabBarControllerSelected" object:nil userInfo:nil];
    
}

- (void)didClickShangChengButtonNotification {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"DidClickButtonChangeTabBarControllerSelected" object:nil userInfo:nil];
}

- (void)didClickShouJiButton {
    
//    NSLog(@"...");
    
//    NSLog(@"%s",__func__);
    ShouJiChongZhiViewController *chongZhi = [[ShouJiChongZhiViewController alloc] init];
    [self.navigationController pushViewController:chongZhi animated:YES];
}

- (void)didClickZhuanZhangBtn {
    ZhuanZhangViewController *zhuanZhangVC = [[ZhuanZhangViewController alloc] init];
    [self.navigationController pushViewController:zhuanZhangVC animated:YES];
}

- (void)loadTopImage:(NSString *)str{
#warning 头部图片
    NSDictionary *params = @{
                             @"PageIndex":@"1",
                             @"PageSize":@"4"
                             };
    
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"QUERYMERCHANT" completeBlock:^(id object) {
       
        NSDictionary *dic = [NetWorkTools dictionaryWithJsonString:object[@"BackData"]];
        NSArray *arr = dic[@"ds"];
        tableArr = arr;
        [self.tableView reloadData];
        [self.tableView reloadInputViews];
        
    }];
    
}



@end
